package com.jingxi.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.common.util.IDUtils;
import com.jingxi.mapper.TbItemDescMapper;
import com.jingxi.mapper.TbItemMapper;
import com.jingxi.mapper.TbItemParamItemMapper;
import com.jingxi.model.TbItem;
import com.jingxi.model.TbItemDesc;
import com.jingxi.model.TbItemExample;
import com.jingxi.model.TbItemParamItem;

@Service
public class ItemServiceImpl implements ItemService{
	
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	
	@Override
	public TbItem getTnItemById(long id) {
		return itemMapper.selectByPrimaryKey(id);
	}

	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		TbItemExample example = new TbItemExample();
		//分页处理
		PageHelper.startPage(page, rows);
		List<TbItem> list = itemMapper.selectByExample(example);
		//创建一个返回值对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public JingXiResult createItem(TbItem item,String desc,String itemParams) {
		Date date = new Date();
		long id = IDUtils.genItemId();
		item.setId(id);
		item.setStatus((byte)1);
		item.setCreated(date);
		item.setUpdated(date);
		itemMapper.insert(item);//添加商品
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(id);
		tbItemDesc.setCreated(date);
		tbItemDesc.setUpdated(date);
		tbItemDesc.setItemDesc(desc);
		itemDescMapper.insert(tbItemDesc);//添加商品描述
		
		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(id);
		tbItemParamItem.setParamData(itemParams);
		tbItemParamItem.setCreated(date);
		tbItemParamItem.setUpdated(date);
		itemParamItemMapper.insert(tbItemParamItem);//添加商品规格参数
		return JingXiResult.ok();
	}
	
	@Override
	public JingXiResult getDescFromId(long id) {
		JingXiResult jingXiResult = JingXiResult.ok(itemDescMapper.selectByPrimaryKey(id));
		return jingXiResult;
	}
	
	@Override
	public JingXiResult updateItem(TbItem tbItem,String desc) {
		Date date = new Date();
		//更新商品信息
		tbItem.setUpdated(date);
		itemMapper.updateByPrimaryKeySelective(tbItem);
		//更新商品描述
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(tbItem.getId());
		tbItemDesc.setUpdated(date);
		tbItemDesc.setItemDesc(desc);
		itemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
		return JingXiResult.ok();
	}

	@Override
	public JingXiResult deleteItem(long[] ids) {
		for(long id:ids) {
			itemMapper.deleteByPrimaryKey(id);//删除商品信息
			itemDescMapper.deleteByPrimaryKey(id);//删除商品描述
		}
		return JingXiResult.ok();
	}

	
}
